package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.BannerQuery;
import cn.wolfcode.wolf2w.repository.BannerRepository;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.uitl.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BannerServiceImpl implements IBannerService {

    @Autowired
    private BannerRepository bannerRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Override
    public void save(Banner banner) {
        banner.setId(null);
        bannerRepository.save(banner);
    }

    @Override
    public void delete(String id) {
        bannerRepository.deleteById(id);
    }

    @Override
    public void update(Banner banner) {
        bannerRepository.save(banner);
    }

    @Override
    public Banner get(String id) {
        return bannerRepository.findById(id).orElse(null);
    }

    @Override
    public List<Banner> lists() {
        return bannerRepository.findAll();
    }
    //分页查询
    @Override
    public Page<Banner> query(BannerQuery qo) {
        //创建查询对象
        Query query = new Query();
        //通过查询拿到总条数
        long count = mongoTemplate.count(query, Banner.class);
        //判断有没有数据
        if (count==0){
            return Page.empty();//返回一个空,页面不会错,如果直接返回一个0页面就会报错
        }
        //无条件分页
        Pageable request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        //查询所有数据
        return DBHelper.query(mongoTemplate,Banner.class,query,request);
    }

    @Override
    public Map<String, Object> getAllTypeMap() {
        //创建一个空的map
        Map<String, Object> map=new HashMap<>();
        //拿的banner中的所有能够显示的攻略
        List<Strategy> sts = strategyService.lists();
        //拿的banner中的所有能够显示的游记
        List<Travel> ts = travelService.lists();
        map.put("sts",sts);
        map.put("ts",ts);
        return map;
    }

    @Override
    public void saveOrUpdate(Banner banner) {
        if (StringUtils.hasLength(banner.getId())){
            this.update(banner);
        }else {
            this.save(banner);
        }
    }

    @Override
    public List<Banner> travelQuer(int state,int type) {
        Pageable pageable =PageRequest.of(0,5, Sort.Direction.ASC,"sequence");
        List<Banner> list =bannerRepository.findByStateAndType(state,type,pageable);
        return list;
    }


}
